package com.yls.except.util;

import com.yls.except.util.imgfile.OssUpload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 读取指定文件夹下的文件
 * @author xusheng
 * @time 2015-7-21 上午10:43:44   
 * @version 1.0
 * @Description:  {}
 *
 */
public class ReadFile {
	public static List<Object> getFile(String path) {
		// get file list where the path has
		File file = new File(path);
		// get the folder list
		File[] array = file.listFiles();
		List<Object> list = new ArrayList<Object>();
		if(array != null) {
			for (int i = 0; i < array.length; i++) {
				if (array[i].isFile()) {
					// only take file name
//					System.out.println("name" + array[i].getName());
					// take file path and name
//					System.out.println("i" + array[i]);
					// take file path and name
//					System.out.println("path" + array[i].getPath());
					list.add(array[i].getPath());
				} else if (array[i].isDirectory()) {
					getFile(array[i].getPath());
				}
			}
		}
		return list;
	}

	public static ArrayList<File> getListFiles(Object obj) {
		File directory = null;
		if (obj instanceof File) {
			directory = (File) obj;
		} else {
			directory = new File(obj.toString());
		}
		ArrayList<File> files = new ArrayList<File>();
		if (directory.isFile()) {
			files.add(directory);
			return files;
		} else if (directory.isDirectory()) {
			File[] fileArr = directory.listFiles();
			for (int i = 0; i < fileArr.length; i++) {
				File fileOne = fileArr[i];
				files.addAll(getListFiles(fileOne));
			}
		}
		return files;
	}

	public static void main(String[] args) {
		List<File> list = getListFiles("D:\\data\\");
//		System.out.println(list.size());
//		String file = "D:\\data\\img\\authimg\\2016\\09\\05\\1121_67824645-6020-4af6-82c7-5c046e61c542.jpg";

//		String filepath = list.get(0).getPath();
//		String real = filepath.replace("D:\\", "").replace("\\", "/");
//		InputStream in = null;
//		try {
//			in = new FileInputStream(filepath);
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}
//		OssUpload.UpImgToAlisOss(real, in, "image/jpeg");

		for(File file: list) {
			String filepath = file.getPath();
			String real = filepath.replace("D:\\", "").replace("\\", "/");
			InputStream in = null;
			try {
				in = new FileInputStream(filepath);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			OssUpload.UpImgToAlisOss(real, in, "image/jpeg");
		}
	}
}
